package com.small.xx.ai.mcp.client.base;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * server端 spring.ai.mcp.server.stdio= false
 */
public class XxAiMcpClientBaseWebFlusTests {

    public static void main(String[] args) {
        var transport = new WebFluxSseClientTransport(WebClient.builder().baseUrl("http://localhost:9999"));
        McpSyncClient client = McpClient.sync(transport).build();

        client.initialize();
        System.out.println("是否已经初始化:" + client.isInitialized());

        McpSchema.ListToolsResult listToolsResult = client.listTools();
        List<McpSchema.Tool> tools = listToolsResult.tools();
        System.out.println(
            "获取到的tools:" + tools.stream().map(McpSchema.Tool::name).collect(Collectors.joining(",")));
        System.out.println();
        for (McpSchema.Tool tool : tools) {
            McpSchema.JsonSchema jsonSchema = tool.inputSchema();
            System.out.println("工具：==========");
            System.out.println("  " + jsonSchema);
            McpSchema.CallToolResult callToolResult = client.callTool(
                new McpSchema.CallToolRequest(tool.name(), Map.of("city", "北京", "timeZoneId", "Europe/Moscow")));

            System.out.println("获取到的结果为：==========");
            callToolResult.content().forEach(v -> System.out.println("  " + v));
            System.out.println();
        }

    }
}